package com.smcdd.cdb.dbo.cdb_dbo.ui.adapter;

import android.content.Context;
import android.net.wifi.ScanResult;
import android.net.wifi.WifiManager;
import android.support.v7.widget.RecyclerView;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ImageView;
import android.widget.TextView;

import com.smcdd.cdb.dbo.cdb_dbo.R;
import com.smcdd.cdb.dbo.cdb_dbo.ui.widget.HintDialog;

import java.util.ArrayList;
import java.util.List;

/**
 * 作者：Administrator on 2017/6/20 14:20
 * 邮箱：zhanghuaiha@gmail.com
 */

public class WifiAdapter extends RecyclerView.Adapter<WifiAdapter.ViewHolder> {
    private Context mContext;
    private OnItemClickListenter onItemClickListenter;
    private List<ScanResult> list;
    private int level;
    private HintDialog selfDialog;

    public WifiAdapter(Context context){
        mContext = context;
        list = new ArrayList<>();
    }

    @Override
    public ViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {
        View view = LayoutInflater.from(parent.getContext()).inflate(R.layout.item_wifi,parent,false);
        return new ViewHolder(view);

    }

    @Override
    public void onBindViewHolder(ViewHolder holder, int position) {
        ScanResult scanResult = list.get(position);
        holder.ssid.setText(scanResult.SSID);
        level = WifiManager.calculateSignalLevel(scanResult.level,5);
        if(scanResult.capabilities.contains("WEP")||scanResult.capabilities.contains("PSK")||
                scanResult.capabilities.contains("EAP")){
            holder.wifi_level.setImageResource(R.drawable.wifi_signal_lock);
        }else{
            holder.wifi_level.setImageResource(R.drawable.wifi_signal_open);
        }
        holder.wifi_level.setImageLevel(level);

        holder.itemView.setTag(position);
        holder.itemView.setOnClickListener(v -> {
            onItemClickListenter.itemClickListenter(list.get(position),position);
        });
    }

    @Override
    public int getItemCount() {
        return list == null? 0 : list.size();
    }


    class ViewHolder extends RecyclerView.ViewHolder{
        ImageView wifi_level;
        TextView ssid;

        public ViewHolder(View itemView) {
            super(itemView);
            wifi_level = (ImageView) itemView.findViewById(R.id.wifi_level);
            ssid = (TextView) itemView.findViewById(R.id.ssid);
        }
    }


    public void Dialog() {
        if (list.size() == 0) {
            selfDialog = new HintDialog(mContext);
            selfDialog.setTitle("提示");
            selfDialog.setMessage("未扫描到附近设备wifi..请重试");
            selfDialog.show();
        }
    }



    public void addData(List<ScanResult> scan){
        if (list != null) {
            list.clear();
            list.addAll(scan);
        }
        notifyDataSetChanged();
    }

    public interface OnItemClickListenter {
        void itemClickListenter(ScanResult scan,int position);
    }

    public void setItemClickListenter(OnItemClickListenter itemClickListenter) {
        onItemClickListenter = itemClickListenter;
    }

}
